import { useState } from 'react';
import lodash from 'lodash';
import moment from 'moment';

interface useStepTargetState {
  setPackData: React.Dispatch<React.SetStateAction<Record<string, any>[]>>;
  packData: Record<string, any>[];
  page: number;
  methods: any;
}

/**
 * @useStepTarget 标的信息配置
 * @param packData 标包数据
 * @param setPackData 设置标包数据
 * @param page 标包页数
 * @param methods 表单方法
 * @returns targetDataSource 标的数据
 * @returns setTartetDataSource 设置标的数据
 * @returns selectedRowKeys 选中标的keys
 * @returns selectedRows 选中标的内容
 * @returns rowSelection 标的表格多选配置
 * @returns onEditTable 编辑标的数据
 */
export const useStepTarget = () => {
  // 标的表格数据
  const [targetDataSource, setTartetDataSource] = useState<Record<string, any>[]>([]);

  // 表格选中行的key
  const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
  const [selectedRows, setSelectedRows] = useState<Record<string, any>[]>([]);

  // 表格复选框配置
  const rowSelection = {
    selectedRowKeys,
    onChange: (selectedRowKeys: React.Key[], selectedRows: Record<string, any>[]) => {
      setSelectedRowKeys(selectedRowKeys);
      setSelectedRows(selectedRows);
    },
  };

  /**
   * @onEditTable 物料表格编辑事件
   * @param value 修改后的值
   * @param type 修改元素标签类型， 区分标签做不同的数据修改
   * @param key 表格key值
   * @param record 表格单条数据
   */
  // const onEditTable = ({ value, type, key, record }: NeedUpdateAPI.IUpdateEditTableState) => {
  //   const tempData = lodash.cloneDeep(packData);
  //   const dataSource = tempData[page - 1].mpurchaseNeedMaterials;

  //   const index = dataSource.findIndex((item: Record<string, any>) => item.id == record.id);
  //   const currentData = lodash.cloneDeep(dataSource);
  //   if (type == 'switch') {
  //     currentData[index][key] = Number(value);
  //   } else if (type == 'InputNumber') {
  //     // 修改表格数量。重新计算预告总价
  //     if (key == 'expectNum') {
  //       currentData[index].expectNum = value;
  //       currentData[index].expectSum = ((value as number) * record.expectPrice).toFixed(2);
  //     }

  //     // 修改表格预估单价。重新计算预告总价
  //     if (key == 'expectPrice') {
  //       currentData[index].expectPrice = value;
  //       currentData[index].expectSum = ((value as number) * record.expectNum).toFixed(2);
  //     }
  //   } else if (type == 'TimePicker') {
  //     currentData[index][key] = value ? moment(value as any).format('YYYY-MM-DD HH:mm:ss') : null;
  //   } else if (type == 'UploadFormCommon') {
  //     // 上传文件
  //     currentData[index][key] = value;
  //   }
  //   tempData[page - 1].mpurchaseNeedMaterials = currentData;

  //   // 计算标的预算总价
  //   const prices = currentData.map((item: Record<string, any>) => {
  //     return item.expectSum;
  //   });

  //   const expectSum = prices?.reduce((prePrice: number, curPrice: number) => {
  //     return Number(prePrice) + Number(curPrice);
  //   }).toFixed(2);

  //   setFieldsValue({ expectSum });

  //   // 同步其他价格
  //   tempData[page - 1]['expectSum'] =
  //     tempData[page - 1]['planSum'] =
  //     tempData[page - 1]['factSum'] =
  //     tempData[page - 1]['needSum'] =
  //     expectSum;
  //   tempData[page - 1]['mpurchaseSource']['expectSum'] = expectSum;

  //   setTartetDataSource(currentData);
  //   setPackData(tempData);
  // };

  return {
    targetDataSource,
    setTartetDataSource,
    selectedRowKeys,
    selectedRows,
    rowSelection,
    // onEditTable,
  };
};
